package com.wise.service.portal.content.applicationarticles;

import com.wise.entities.content.ApplicationArticle;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.summer.v1.portal.content.applicationarticles.SearchApplicationArticles;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.Arrays;
import java.util.List;

/**
 * @author hejiawei
 * @date 2020/10/27 12:54
 */
@Service
@SummerService
public class SearchApplicationArticlesService implements SummerServiceBean<SearchApplicationArticles> {
    @Override
    public void sum(SearchApplicationArticles summer) {
        SqlSelect sqlSelect = SqlSelect.selec("*")
                .fromClassName(ApplicationArticle.class)
                .eqLang(ApplicationArticle._lang)
                .eq(ApplicationArticle._state, "1")
                .eq(StringUtils.isNotBlank(summer.getColumnId()), ApplicationArticle._columnId, summer.getColumnId());
        if (StringUtils.isNotBlank(summer.getTitle())) {
            //分词
            List<String> terms = Arrays.asList(StringUtils.split(summer.getTitle(), " "));
            for (int i = 0; i < terms.size(); i++) {
                sqlSelect.like(ApplicationArticle._title, "%" + terms.get(i) + "%");
                if (terms.size() - i > 1) {
                    sqlSelect.or();
                }
            }
//            List<Term> terms = AnsjUtil.getAnsj(summer.getTitle());
//            for (int i = 0; i < terms.size(); i++) {
//                sqlSelect.like(ApplicationArticle._title, "%" + terms.get(i).getName() + "%");
//                if (terms.size() - i > 1) {
//                    sqlSelect.or();
//                }
//            }
        }
        sqlSelect.order_by(ApplicationArticle._sort)
                .order_by(ApplicationArticle._publishTime);

        List<ApplicationArticle> resultList = DaoList.s(ApplicationArticle.class, sqlSelect);

        summer.setSummerResult(resultList);
    }
}
